/*
 * @Author: wenyu
 * @Date:   2017-01-09 17:26:22
 * @Last Modified by:   wenyu
 * @Last Modified time: 2017-01-13 08:46:38
 */

'use strict';

var Comps = {},
    CompsLoader = {},
    CompMessage = {},
    Utils = {},
    isLoadedData = [];

CompMessage = {
    topics:{},
    _genName:function(name){
        return "t."+ escape(name);
    },
    subscribe:function(topic,func){
        var name = this._genName(topic);
        if(this.topics[name]){
            this.topics[name].push(func);
        }else{
            this.topics[name] = [];
            this.topics[name].push(func);
        }
    },
    publish:function(topic,option,callback){
        console.log("发布消息:"+topic);
        if(option==null || option == undefined){
            option = {};
        }
        var name = this._genName(topic);
        for(var i in this.topics[name]){
            this.topics[name][i](option,callback);
        }
    }
};

Utils.loader = function(funcName, option) {
    // if (!option.hasOwnProperty('compsType')) {
    //     var moduleFuncName = funcName.toLowerCase();
    //     require([moduleFuncName], function(moduleFuncName) {
    //         var obj = new Comps[funcName](option);
    //         obj.init();
    //     });
    // }
    //
    var obj = new Comps[funcName](option);
    obj.init();

    isLoadedData.push(option.compId);
    // console.info('%c' + funcName + "组件已经被注册，组件id为" + option.compId, 'color:' + Utils.getRandomColor());

}

Utils.ajaxLoadComplate = function(target) {
    $("#" + target.compId).find('.m-comsloading').hide();
}

Utils.loadComplate = function(target) {
    $("#" + target.compId).find('.m-comsloading').hide();
}

Utils.throttle = function(fn, delay) {
    var timer = null;
    return function() {
        var context = this,
            args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function() {
            fn.apply(context, args);
        }, delay);
    };
}

Utils.getRandomColor = function() {
    return '#' + (Math.random() * 0xffffff << 0).toString(16);
}
